<?php
exit;
/**
 * @param unknown_type $str
 * 存在-并且 -后面不是字母
 * @return true 是后缀  false不是后缀
 */
$data=array();
function isFix($str){
	$str = trim($str);
	if (substr($str, 0,1)=="-")
	return true;
	return false;
	
}

function readFix($i){
	global $lines,$data;
	$str = trim($lines[$i]);
	if (strstr($str, "①")){ //echo "a";
		//多
	$pos=strpos($str, "[");
	$root =substr($str, 0,$pos-3);//root 使用旧的
	$class=substr($str,$pos+1,strlen($str)-2-$pos);
	$j=$i;
		for(;;$i++){
			$str = trim($lines[$i]);
			if ((strstr($str, "①")||strstr($str, "②")||strstr($str, "③")||strstr($str, "④")||strstr($str, "⑤"))&& (substr($str, 0,1)!="-" ||$j==$i)){
				
				$pos=strpos($str, "[");
				$class=substr($str,$pos+1,strlen($str)-2-$pos);//每次新的
			//开始读取 解释 多或者单
			$in=readEx($i+1);
			$data[$root][$class]=$in[1];
			$i= $in[0]-1;
			}else{
				return $i;
			}
		}
	}else{
		//单
		$pos=strpos($str, "[");
		$root =substr($str, 0,$pos);
		$class=substr($str,$pos+1,strlen($str)-2-$pos);
		//开始读取 解释 多或者单
		$in=readEx($i+1);
		$data[$root][$class]=$in[1];
		return $in[0];	
	}
}

function readEx($i){
	global $lines,$data;
	$array=array();
	$info = trim($lines[$i]);//
	if (is_numeric(substr($info, 0,1))){
		//多条 直到下一个-
		$p=trim(substr($info, 4));
		for(;;$i++){
			$info = trim($lines[$i]);
			if(substr($info,0,1)=='-' ){
				break;
			}
			if (is_numeric(substr($info, 0,1))){
				$p=trim(substr($info, 4));
				continue;
			}else{
				$tt = preg_split("/\s+/", $info);
				$array[$p][]=array($tt[0],trim(substr($info, strlen($tt[0]))));
			}
		}
	}
	else{
		//单条 直到下一个-
		for($i++;$i<count($lines);$i++){
			$tmp = trim($lines[$i]);
			if(substr($tmp,0,1)=='-' || strstr($tmp, "①")||strstr($tmp, "②")||strstr($tmp, "③")||strstr($tmp, "④")||strstr($tmp, "⑤")){
				break;
			}
			$tt = preg_split("/\s+/", $tmp);
			$array[$info][]=array($tt[0],trim(substr($tmp, strlen($tt[0]))));
		}
	}
	return array($i,$array);
}

$dir=__DIR__."/hz";
$contents= file_get_contents($dir);
$contents=trim($contents);
$lines=explode("\n", $contents);
$tNum = count($lines);
for ($i=0;$i<$tNum;){//echo $i."\n";
	
	if (isFix($lines[$i])){
		$i=readFix($i);	
	}elseif ($i<$tNum){
		die("错误发生").$i." --";
	}
	if($i>3200){
		$j=0;
		$j++;
	}
}
echo "读取完毕";
print_r(array_keys($data));
print_r(($data));
echo count($data);

//exit;
mysql_connect("localhost","root","");
mysql_select_db("diandi_danci");
mysql_query("set names utf8");
foreach ($data as $root=> $row){
	mysql_query("INSERT INTO `diandi_danci`.`suffix` (`id`, `type`, `affix`, `info`) VALUES (NULL, '', '".$root."', '');");
	$rootId= mysql_insert_id();
	foreach ($row as $class =>$r){
		mysql_query("INSERT INTO `diandi_danci`.`suffix_class` (`id`, `suffix_id`, `info`) VALUES (NULL, '".$rootId."', '".$class."');");
		$classId= mysql_insert_id();
		foreach ($r as $info=>$lines){
			mysql_query("INSERT INTO `diandi_danci`.`suffix_info` (`id`, `suffix_id`, `suffix_class_id`, `info`) VALUES (NULL, '".$rootId."', '".$classId."', '".$info."');");
			$infoRootId=mysql_insert_id();
			foreach ($lines as $word){
				//$word[0]//1
				$re = mysql_query("select * from word where word='".$word[0]."'");
				if ($r=mysql_fetch_array($re)){
					$wordId=$r['id'];
				}else{
					mysql_query("INSERT INTO `diandi_danci`.`word` (`id`, `word`, `symbolA`, `symbolE`, `pronunciationA`, `pronunciationE`) VALUES (NULL, '".$word[0]."', '', '', '', '');");
					$wordId=mysql_insert_id();
				}
				mysql_query("INSERT INTO `diandi_danci`.`word_suffix` (`id`, `word_id`, `suffix_id`, `info`, `suffix_info_id`, `suffix_class_id`) VALUES (NULL, '".$wordId."', '".$rootId."', '".$word[1]."', '".$infoRootId."', '".$classId."');");
			}
		}
	}
	
	
	
}